﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;

namespace CMS.BLL
{
    public class HotelBooking
    {
        public eHotelBooking Data;

        public HotelBooking()
        {
            Data = new eHotelBooking();
        }

        ~HotelBooking()
        {
        }

        public virtual void Dispose()
        {
        }

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cHotelBooking objDAL = new cHotelBooking();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cHotelBooking objDAL = new cHotelBooking();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
            cHotelBooking objInfor = new cHotelBooking();
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.HotelBookingID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public int Update(int ID)
        {
            int nResult = 0;
            cHotelBooking objInfor = new cHotelBooking();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        private void MappingData(cHotelBooking objInfor)
        {
            objInfor.BookingDate = Data.BookingDate;
            objInfor.CheckIn = Data.CheckIn;
            objInfor.CheckOut = Data.CheckOut;
            objInfor.CountryID = Data.CountryID;

            try { objInfor.Email = Data.Email; }
            catch { objInfor.Email = ""; }
            try { objInfor.FullName = Data.FullName; }
            catch { objInfor.FullName = ""; }
            
            objInfor.HotelID = Data.HotelID;
            objInfor.NumberOfExtraBed = Data.NumberOfExtraBed;
            objInfor.NumberOfRoom = Data.NumberOfRoom;
            objInfor.PaymentID = Data.PaymentID;
            try { objInfor.Phone = Data.Phone; }
            catch { objInfor.Phone = ""; }
            try { objInfor.QuestionsAndComments = Data.QuestionsAndComments; }
            catch { objInfor.QuestionsAndComments = ""; }
            objInfor.RoomClass = Data.RoomClass;
            objInfor.Status = Data.Status;
        }

        private void ConvertData(cHotelBooking objInfor)
        {
            Data.BookingDate = objInfor.BookingDate;
            Data.CheckIn = objInfor.CheckIn;
            Data.CheckOut = objInfor.CheckOut;
            Data.CountryID = objInfor.CountryID;
            Data.Email = objInfor.Email;
            Data.FullName = objInfor.FullName;
            Data.HotelID = objInfor.HotelID;
            Data.NumberOfExtraBed = objInfor.NumberOfExtraBed;
            Data.NumberOfRoom = objInfor.NumberOfRoom;
            Data.PaymentID = objInfor.PaymentID;
            Data.Phone = objInfor.Phone;
            Data.QuestionsAndComments = objInfor.QuestionsAndComments;
            Data.RoomClass = objInfor.RoomClass;
            Data.Status = objInfor.Status;
        }
        public int UpdateStatus(int ID, int Status)
        {
            int nResult = 0;
            cHotelBooking objDAL = new cHotelBooking();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.Status = Status;

                objDAL.Save();
                nResult = objDAL.HotelBookingID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }
        public DataTable Search(int ItemID, int ItemView, int PageIndex, int PageSize, ref int PageCount)
        {
            DataTable tbResult = new DataTable();
            cHotelBooking objDAL = new cHotelBooking();
            tbResult = objDAL.Search(ItemID, ItemView, PageIndex, PageSize, ref PageCount);
            return tbResult;
        }
    }
}